import {routerRedux} from 'dva/router';
import api from '../api';

import {message} from 'antd'




export default {
  namespace : 'periodReplay',
  state : {
    // traderListData:[],
    // stocklistData:[],
    // pnlListData:[],
    // tradeYieldListData:[],
    // marketYieldListData:[],
    // utilizationListData:[],
    // fund: 'total', // 基金ID 默认值：total
  },
  subscriptions : {
		setup({dispatch, history}) {
			history.listen(({ pathname, query }) => {
				// 没有参数时，向后端请求数据，并且改变query
				if( pathname === '/tradeReplay/periodReplay' ){
					dispatch({type: 'init',payload:query,pathname})
				}
			})
		}
	},

  effects : {

    * init({
				payload
			}, {call, put,select}) {

        // 初始化
				const state=yield select(state => state.periodReplay)
				// 看本次的参数和上次的参数是否一致：
				// 一致的话，不请求，不一致的话，正常请求
				// 定义一个对象用于定义参数
				const newObj={}
				for(var key in payload){
					newObj[key]=state[key]
				}
				if(!Object.getOwnPropertyNames(payload).length||JSON.stringify(payload)!=JSON.stringify(newObj)){
          yield put({
						type:'setState',
						payload,
					})
        }

		},

    * getTeamSumStat({
      payload
    }, {call, put, select}) {
      // console.log('getTeamSumStat',payload)
      const query={}
      payload&&payload.fund && (payload.fund != 'total') && (query.groupid = payload.fund)
      payload&&payload.begindate&&(query.begindate=payload.begindate)
      payload&&payload.enddate&&(query.enddate=payload.enddate)
      const res=yield call(api.replay.getTeamSumStat,query)
      if(res.code===0){
        const teamSumStat=res.data
        yield put({
          type:'setState',
          payload:{
            teamSumStat,
          }
        })
        
      }else{
        message.info('周期复盘数据查询失败！')
      }

    },
    * getTraderstatlists({
      payload
    }, {call, put, select}) {
      const traderStatLists=(yield call(api.replay.getTraderstatlists,payload)).data
      const traderStatListsData=[]
      yield put({
        type:'setState',
        payload:{
          traderstatlists,
        }
      })
    },
    * getGroupDefinitionList({payload},{call,put}){
      const res = yield call(api.replay.getGroupDefinitionList)
      // console.log('getGroupDefinitionList11',res)
      if (res.code === 0) {
        const fundList = res.data.list
        const date=res.data.date
        yield put({type: 'setState', payload: {
            fundList,
            ...date
          }})
      } else {
        message.info('获取基金列表数据失败！')
      }
    },
    * changeRouter({payload}, {put}){
      // console.log(99,location);
			yield put(routerRedux.push(payload))
		}
  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    },
  }
}
